import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useUserStore = defineStore("user", ()=>{
  const userMsg = localStorage.getItem("userMsg") ? JSON.parse(localStorage.getItem("userMsg")) : {};

  console.log(userMsg);

  const token = ref(userMsg.token);
  const uId = ref(userMsg.uId);
  const tId = ref(userMsg.tId);
  const power = ref(userMsg.power);
  const uName = ref(userMsg.uName);

  function change_power(v){
    localStorage.setItem("userMsg", JSON.stringify({
      ...userMsg,
      power: v
    }))
    power.value = v;
  }
  function change_uName(v){
    localStorage.setItem("userMsg", JSON.stringify({
      ...userMsg,
      uName: v
    }))
    uName.value = v;
  }
  function change_uId(v){
    localStorage.setItem("userMsg", JSON.stringify({
      ...userMsg,
      uId: v
    }))
    uId.value = v;
  }
  function change_tId(v){
    localStorage.setItem("userMsg", JSON.stringify({
      ...userMsg,
      tId: v
    }))
    tId.value = v;
  }
  function change_token(v){
    localStorage.setItem("userMsg", JSON.stringify({
      ...userMsg,
      token: v
    }))
    token.value = v;
  }

  return { token, uId, tId, power, uName, change_power, change_token, change_tId, change_uId, change_uName };
})